package cn.diancan.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.core.io.ClassPathResource;

import cn.diancan.vo.other.Setting;
 

public class ShopCountUtil {
	
	public static Long COUNT=1383276l;
	
	static{
		 
	}

	public static Long getCount() {
		
		return COUNT;
	}
	
	/**
	 * 正常时段
	 * @return
	 */
	public static Integer getAdd1(){
		int gl= new Random(System.currentTimeMillis()).nextInt(7)+1; 
		Integer add=new Random().nextInt(gl*2); 
		if(null==add || 0>=add){
			add=0;
		}
		return add;
	}
	
	/**
	 * 凌晨
	 * @return
	 */
	public static Integer getAdd2(){
		int gl= new Random(System.currentTimeMillis()).nextInt(30)+1; 
		Integer add= 0;
		if(gl==3){
			add=new Random().nextInt(3);
		} 
		if(0>=add){
			add=0;
		}
		return add;
	}
	


	public static void setCount(Long count) {
		COUNT = count; 
			 try {
				String path= new ShopCountUtil().getClass().getClassLoader().getResource("").getPath()+ "count.m";
				ByteArrayInputStream is = new ByteArrayInputStream((""+count).getBytes("UTF-8"));  
				ReadTxtFile.writeXmlFile(is, path);
			} catch (UnsupportedEncodingException e) { 
				e.printStackTrace();
			} 
		/* try {
			  String filename = "freeweidian.xml";  
			   
				File file = new ClassPathResource(filename).getFile();
				if (!file.exists()) {  
				      System.out.println("  Error : Config file doesn't exist!");  
				      System.exit(1);  
				  }  
				 
				  Document   document = (new SAXReader()).read(file);  
				  List<Element> list = document.selectNodes("/freeweidian/setting");
				  Iterator<Element> iterator = list.iterator();
				  
				  while(iterator.hasNext()){
				  	Element element1 = iterator.next();
				  	String sName = element1.attributeValue("name");
				    if(null!= sName && "shopCount".equals(sName) ){
				    	element1.setAttributeValue("value", ""+COUNT); 
				    }
				  }
				//输出全部原始数据，在编译器中显示  
			        OutputFormat format = OutputFormat.createPrettyPrint();  
			        format.setEncoding("UTF-8");
			        // 输出全部原始数据，并用它生成新的我们需要的XML文件  
			        XMLWriter writer2 = new XMLWriter(new FileWriter(file), format);  
			        writer2.write(document); //输出到文件  
			        writer2.close();
			} catch (Exception e) { 
				e.printStackTrace();
			}	*/ 
	}
/* 	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			 System.out.println(getAdd1());
		}
	
	} */
	 
	
/*	public static Long getCount(){
		String path= new ShopCountUtil().getClass().getClassLoader().getResource("").getPath()+ "count.m";
		String count = ReadTxtFile.readTxtFile(path);
		if(null!=count && !"".equals(count.trim())){
			try {
				COUNT= Long.valueOf(count); 
			} catch (NumberFormatException e) { 
				e.printStackTrace(); 
			} 
		} 
		return COUNT;
	}
	
	public static void setCount(Long count){
		try {
			String path= new ShopCountUtil().getClass().getClassLoader().getResource("").getPath()+ "count.m";
			ByteArrayInputStream is = new ByteArrayInputStream((""+count).getBytes("UTF-8"));  
			ReadTxtFile.writeXmlFile(is, path);
		} catch (UnsupportedEncodingException e) { 
			e.printStackTrace();
		} 
	}*/

}
